package com.hera.base.web.vaadin;

import java.io.Serializable;
import java.util.Collection;

import com.hera.base.util.Constantes;
import com.hera.base.web.vaadin.mantenimiento.CalendarUI;
import com.hera.base.web.vaadin.mantenimiento.DatasourceUI;
import com.hera.base.web.vaadin.mantenimiento.DepartmentUI;
import com.hera.base.web.vaadin.mantenimiento.FieldOptionUI;
import com.hera.base.web.vaadin.mantenimiento.LoginMethodUI;
import com.hera.base.web.vaadin.mantenimiento.ParameterUI;
import com.hera.base.web.vaadin.mantenimiento.ProcessGroupUI;
import com.hera.base.web.vaadin.mantenimiento.ProcessUI;
import com.hera.base.web.vaadin.mantenimiento.SecurityGroupUI;
import com.hera.base.web.vaadin.mantenimiento.SysUserUI;
import com.hera.process.config.domain.Option;
import com.vaadin.ui.Component;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

public class OpcionMenuCommand implements com.vaadin.ui.MenuBar.Command, Serializable {

	private static final long serialVersionUID = 1L;
	Option option;

	public OpcionMenuCommand(Option option) {
		this.option = option;
	}

	private boolean existeComponenteAbierto(String codigo) {
		Collection<Window> lstWindowsOpen = UI.getCurrent().getWindows();
		for (Window subWindow : lstWindowsOpen) {
			if (subWindow.getId().equals(codigo)) {
				subWindow.focus();
				return true;
			}
		}
		return false;
	}

	private void mostrarPantalla(Option option, Component component, String alto, String ancho) {
		Window window = new Window();
		window.setId(option.getCode());
		window.setStyleName("titulo-bold");
		window.setModal(false);
		window.setResizable(false);
		window.setCaption(option.getName());
		VerticalLayout layout = new VerticalLayout();
		layout.addComponent(component);
		layout.setMargin(true);
		window.setContent(layout);
		window.setHeight(alto);
		window.setWidth(ancho);
		UI.getCurrent().addWindow(window);
	}

	@Override
	public void menuSelected(MenuItem selectedItem) {
		if (existeComponenteAbierto(option.getCode())) {
			return;
		}
		if (option.getCode().equals(Constantes.MENU.PARAMETER)) {
			ParameterUI panel = new ParameterUI();
			mostrarPantalla(option, panel, "600", "820");
		}else if (option.getCode().equals(Constantes.MENU.DATA_SOURCE)) {
			DatasourceUI panel = new DatasourceUI();
			mostrarPantalla(option, panel, "330", "720");
		}else if(option.getCode().equals(Constantes.MENU.PROCESS_GROUP)){
			ProcessGroupUI panel=new ProcessGroupUI();
			mostrarPantalla(option, panel, "600", "730");
		}else if (option.getCode().equals(Constantes.MENU.PROCESS)) {
			ProcessUI panel = new ProcessUI();
			mostrarPantalla(option, panel, "350", "730");
		}else if(option.getCode().equals(Constantes.MENU.DEPARTMENT)){
			DepartmentUI panel=new DepartmentUI();
			mostrarPantalla(option, panel, "600", "810");
		}else if(option.getCode().equals(Constantes.MENU.CALENDAR)){
			CalendarUI panel=new CalendarUI();
			mostrarPantalla(option, panel, "650", "820");
		}
		else if(option.getCode().equals(Constantes.MENU.LOGIN_METHOD)){
			LoginMethodUI panel=new LoginMethodUI();
			mostrarPantalla(option, panel, "350", "830");
		}
		else if(option.getCode().equals(Constantes.MENU.USUARIO)){
			SysUserUI panel=new SysUserUI();
			mostrarPantalla(option, panel, "550", "820");
		}
		else if(option.getCode().equals(Constantes.MENU.SECURITY_GROUP)){
			SecurityGroupUI panel=new SecurityGroupUI();
			mostrarPantalla(option, panel, "600", "820");
		}
//		else if(option.getCode().equals(Constantes.MENU.FILED_OPTION)){
//			FieldOptionUI panel=new FieldOptionUI();
//			mostrarPantalla(option, panel, "330", "720");
//		}
	}
}
